<template>
  <view class="container">
    <view class="category-list">
      <view class="category-item" v-for="(category, index) in categories" :key="index"
        @click="goToCategoryDetail(category.id)">
        <view class="category-content">
          <view class="category-left">
            <text class="category-name">{{ category.name }}</text>
            <text class="category-count">({{ category.count || 0 }})</text>
          </view>
          <uni-icons type="right" size="16" color="#999"></uni-icons>
        </view>
      </view>
    </view>

    <!-- 加载状态 -->
    <view class="loading-box" v-if="loading">
      <view class="loading">
        <view class="loading-spinner"></view>
        <text>加载中...</text>
      </view>
    </view>

    <!-- 空状态 -->
    <view class="empty-state" v-if="!loading && categories.length === 0">
      <text>暂无分类</text>
    </view>
  </view>
</template>

<script>
import api from '@/api/wordpress'

export default {
  data() {
    return {
      categories: [],
      loading: false
    }
  },

  onLoad() {
    this.getCategories()
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.getCategories()
  },

  methods: {
    async getCategories() {
      this.loading = true
      try {
        const res = await api.getCategories()
        if (res.statusCode === 200 && res.data) {
          // 按文章数量排序
          this.categories = res.data.sort((a, b) => (b.count || 0) - (a.count || 0))
        }
      } catch (e) {
        console.error('获取分类失败:', e)
        uni.showToast({
          title: '获取分类失败',
          icon: 'none'
        })
      } finally {
        this.loading = false
        uni.stopPullDownRefresh()
      }
    },

    goToCategoryDetail(id) {
      uni.navigateTo({
        url: `/pages/category/detail?id=${id}`
      })
    }
  }
}
</script>

<style>
.container {
  padding: 20rpx;
  min-height: 100vh;
  background-color: #f5f6fa;
}

.category-list {
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.category-item {
  border-bottom: 1px solid #f5f5f5;
}

.category-item:last-child {
  border-bottom: none;
}

.category-content {
  padding: 30rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.category-left {
  display: flex;
  align-items: center;
  gap: 10rpx;
}

.category-name {
  font-size: 32rpx;
  color: #333;
  font-weight: 500;
}

.category-count {
  font-size: 28rpx;
  color: #999;
}

/* 加载状态 */
.loading-box {
  padding: 40rpx 0;
  text-align: center;
}

.loading {
  display: inline-flex;
  align-items: center;
}

.loading-spinner {
  width: 40rpx;
  height: 40rpx;
  border: 4rpx solid #f3f3f3;
  border-top: 4rpx solid #ff5722;
  border-radius: 50%;
  margin-right: 16rpx;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

.loading text {
  font-size: 28rpx;
  color: #999;
}

/* 空状态 */
.empty-state {
  padding: 60rpx;
  text-align: center;
  color: #999;
  font-size: 28rpx;
}

/* 点击效果 */
.category-item:active {
  background-color: #f9f9f9;
}
</style> 